package com.yinhe.module.cjl;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;


import com.yinhe.system.domain.YhVacation;
import com.yinhe.system.msg.mssage.domain.YhMsgInner;
import com.yinhe.system.msg.ws.service.WebSocketService;
import com.yinhe.system.service.IYhLeaveService;
import com.yinhe.system.service.IYhVacationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 休假记录Controller
 * 
 * @author yinhe
 * @date 2022-09-04
 */
@RestController
@RequestMapping("/module/cjl/vacation")
public class CjlVacationController extends BaseController
{
    @Autowired
    private IYhVacationService yhVacationService;
    @Autowired
    private IYhLeaveService yhLeaveService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    public WebSocketService webSocketService;

    /**
     * 查询休假记录列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhVacation yhVacation)
    {
        startPage();
        List<YhVacation> list = yhVacationService.selectYhVacationList(yhVacation);
        return getDataTable(list);
    }

    /**
     * 导出休假记录列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:export')")
    @Log(title = "休假记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhVacation yhVacation)
    {
        List<YhVacation> list = yhVacationService.selectYhVacationList(yhVacation);
        ExcelUtil<YhVacation> util = new ExcelUtil<YhVacation>(YhVacation.class);
        util.exportExcel(response, list, "休假记录数据");
    }

    /**
     * 获取休假记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhVacationService.selectYhVacationById(id));
    }

    /**
     * 新增休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:add')")
    @Log(title = "休假记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhVacation yhVacation)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhVacation.setUserId(sysUser.getUserId());
        yhVacation.setDeptId(sysUser.getDeptId());
        yhVacation.setUsername(sysUser.getUserName());
        yhVacation.setIdNumber(sysUser.getIdNumber());
        yhVacation.setCreateBy(getUsername());
        yhVacation.setCreateTime(new Date());
        yhVacation.setLeaveSituation("0");//未销假
        //推送消息给部门，局长和政委
        // 1.组合消息体
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("休假申请");
        msgInner.setMsgContent("已申请休假");
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setNote1(sysUser.getNickName());
        msgInner.setNote2("/zonghe/yygvacation");
        //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
        List<SysUser> result = yhLeaveService.selectUserByDeptId(sysUser.getDeptId());
        SysUser director =  yhLeaveService.selectDirector();
        SysUser commissor =  yhLeaveService.selectCommissor();
        for (SysUser a:result){
            a.getUserName();
            request.getServletContext().setAttribute("userName", a.getUserName());
        }
        String userName=(String) request.getServletContext().getAttribute("userName");
        // 2.组合接收者列表
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add(userName);
        receiveNames.add(director.getUserName());
        receiveNames.add(commissor.getUserName());
        // 3.调用服务
        webSocketService.PushMsg(msgInner, receiveNames);
        return toAjax(yhVacationService.insertYhVacation(yhVacation));
    }

    /**
     * 修改休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:edit')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhVacation yhVacation)
    {
        return toAjax(yhVacationService.updateYhVacation(yhVacation));
    }
    /**
     * 部门阅知休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:view')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/view")
    public AjaxResult view(@RequestBody YhVacation yhVacation) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhVacation.setUpdateBy(getUsername());
        yhVacation.setUpdateTime(new Date());
        yhVacation.setDepartmentHeadName(getUsername());
        yhVacation.setDepartmentHeadIdnum(sysUser.getIdNumber());
        yhVacation.setDepartmentHeadResult("1");
        //推送消息给申请人
        // 1.组合消息体
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("休假申请");
        msgInner.setMsgContent("部门已审阅休假");
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setNote1(sysUser.getNickName());
        msgInner.setNote2("/zonghe/yygvacation");
        //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
        // 2.组合接收者列表
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add(yhVacation.getUsername());
        // 3.调用服务
        webSocketService.PushMsg(msgInner, receiveNames);
        return toAjax(yhVacationService.viewYhVacation(yhVacation));
    }
    /**
     * 局长阅知休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:chief')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/chief")
    public AjaxResult chief(@RequestBody YhVacation yhVacation) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhVacation.setUpdateBy(getUsername());
        yhVacation.setUpdateTime(new Date());
        yhVacation.setDirectorGeneralName(getUsername());
        yhVacation.setDirectorGeneralIdnum(sysUser.getIdNumber());
        yhVacation.setDirectorGeneralResult("1");
        //推送消息给申请人
        // 1.组合消息体
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("休假申请");
        msgInner.setMsgContent("局长已审阅休假");
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setNote1(sysUser.getNickName());
        msgInner.setNote2("/zonghe/yygvacation");
        //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
        // 2.组合接收者列表
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add(yhVacation.getUsername());
        // 3.调用服务
        webSocketService.PushMsg(msgInner, receiveNames);
        return toAjax(yhVacationService.chiefYhVacation(yhVacation));
    }

    /**
     * 政委阅知休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:gaott')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/gaott")
    public AjaxResult gaott(@RequestBody YhVacation yhVacation) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhVacation.setUpdateBy(getUsername());
        yhVacation.setUpdateTime(new Date());
        yhVacation.setPoliticalCommissarName(getUsername());
        yhVacation.setPoliticalCommissarIdnum(sysUser.getIdNumber());
        yhVacation.setPoliticalCommissarResult("1");
        //推送消息给申请人
        // 1.组合消息体
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("休假申请");
        msgInner.setMsgContent("政委已审阅休假");
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setNote1(sysUser.getNickName());
        msgInner.setNote2("/zonghe/yygvacation");
        //msgInner.setContentType("这里写内容类型，可先不填，后续根据需要再确定");
        // 2.组合接收者列表
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        receiveNames.add(yhVacation.getUsername());
        // 3.调用服务
        webSocketService.PushMsg(msgInner, receiveNames);
        return toAjax(yhVacationService.gaottYhVacation(yhVacation));
    }

    /**
     * 销假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:back')")
    @Log(title = "休假记录", businessType = BusinessType.UPDATE)
    @PutMapping("/back")
    public AjaxResult back(@RequestBody YhVacation yhVacation) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhVacation.setUpdateBy(getUsername());
        yhVacation.setUpdateTime(new Date());
        yhVacation.setLeaveSituation("1");
        return toAjax(yhVacationService.backYhVacation(yhVacation));
    }
    /**
     * 删除休假记录
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:vacation:remove')")
    @Log(title = "休假记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhVacationService.deleteYhVacationByIds(ids));
    }
}
